<template>
  <div class="energy-panel">
    <div class="panel-title">能效分析</div>
    <div class="panel-bg-wrapper">
      <div class="panel-content">
        <!-- 顶部时间维度按钮组 -->
        <div class="time-dimension-tabs">
          <div
            v-for="(tab, index) in tabs"
            :key="index"
            class="tab-item"
            :class="{ active: activeTab === index }"
            @click="switchTab(index)"
          >
            {{ tab }}
          </div>
        </div>

        <!-- 用电量统计区域 -->
        <div class="energy-stats-container">
          <div class="stats-header">
            <div class="header-icon">
              <img
                src="@/assets/image/EmergencyPanel/组 8081(1).png"
                alt="统计"
              />
            </div>
            <div class="header-title">用电量统计</div>
            <div class="total-value" style="margin-left: 20px">1120 kw/h</div>
          </div>

          <!-- 用电量统计条形图 - 改回直接使用CSS渐变实现 -->
          <div class="energy-stats">
            <div
              class="stats-item"
              v-for="(item, index) in energyData"
              :key="index"
            >
              <div class="item-label">{{ item.name }}</div>
              <div class="progress-container">
                <div
                  class="progress-bar"
                  :style="{ width: item.value + '%' }"
                ></div>
              </div>
              <div class="item-value">{{ item.kwh }}</div>
            </div>
          </div>
        </div>

        <!-- 用电量趋势区域 -->
        <div class="energy-trend-container">
          <div class="trend-header">
            <div class="header-icon">
              <img
                src="@/assets/image/EmergencyPanel/组 8081(1).png"
                alt="趋势"
              />
            </div>
            <div class="header-title">用电量趋势</div>
          </div>

          <!-- 趋势图 -->
          <div class="trend-chart">
            <div ref="trendChartRef" class="trend-chart-container"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import * as echarts from "echarts";

const tabs = ["天", "月", "季", "年"];
const activeTab = ref(0); // 默认选中"天"

// 引用DOM元素
const trendChartRef = ref(null);
let trendChart = null;

// 模拟数据
const energyData = [
  { name: "照明", value: 60, kwh: "210kw/h" },
  { name: "摄像机", value: 40, kwh: "210kw/h" },
  { name: "交通诱导", value: 50, kwh: "210kw/h" },
  { name: "环境", value: 35, kwh: "210kw/h" },
  { name: "消防", value: 45, kwh: "210kw/h" },
];

function switchTab(index) {
  activeTab.value = index;
  // 根据选中的时间维度加载不同数据
}

// 初始化趋势图
function initTrendChart() {
  if (!trendChartRef.value) return;

  trendChart = echarts.init(trendChartRef.value);

  const option = {
    grid: {
      left: 40,
      right: 20,
      top: 20,
      bottom: 30,
    },
    tooltip: {
      trigger: "axis",
    },
    xAxis: {
      type: "category",
      data: ["7-7", "7-6", "7-5", "7-4", "7-3", "7-2", "7-1"],
      axisLine: {
        lineStyle: {
          color: "rgba(255, 255, 255, 0.3)",
        },
      },
      axisLabel: {
        color: "#fff",
        fontSize: 12,
      },
    },
    yAxis: {
      type: "value",
      min: 0,
      max: 600,
      interval: 150,
      splitLine: {
        lineStyle: {
          color: "rgba(255, 255, 255, 0.1)",
        },
      },
      axisLine: {
        show: false,
      },
      axisLabel: {
        color: "#fff",
        fontSize: 12,
      },
    },
    series: [
      {
        type: "line",
        data: [130, 320, 190, 290, 400, 320, 460],
        smooth: true,
        symbol: "circle",
        symbolSize: 6,
        markPoint: {
          data: [
            {
              type: "max",
              name: "Max",
              symbolSize: 10,
              itemStyle: { color: "#fff" },
            },
          ],
          label: {
            formatter: "{c}",
          },
        },
        lineStyle: {
          width: 3,
          color: "#3EC6FF",
        },
        itemStyle: {
          color: "#3EC6FF",
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: "rgba(62, 198, 255, 0.8)" },
            { offset: 1, color: "rgba(62, 198, 255, 0.1)" },
          ]),
        },
      },
    ],
  };

  trendChart.setOption(option);
}

// 组件挂载时初始化图表
onMounted(() => {
  initTrendChart();

  // 响应窗口大小变化
  window.addEventListener("resize", handleResize);
});

// 组件卸载时销毁图表
onUnmounted(() => {
  if (trendChart) {
    trendChart.dispose();
    trendChart = null;
  }
  window.removeEventListener("resize", handleResize);
});

// 窗口大小变化时重新调整图表大小
function handleResize() {
  if (trendChart) {
    trendChart.resize();
  }
}
</script>

<style lang="scss" scoped>
.energy-panel {
  width: 100%;
  box-sizing: border-box;
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;

  .panel-title {
    background: url("@/assets/image/TunnelOverviewPanel/组 2.png") no-repeat
      center/cover;
    width: 451px;
    height: 43px;
    padding: 3px 0 25px 30px;
    font-size: 18px;
    font-weight: bold;
    color: #fff;
    text-align: left;
  }

  .panel-bg-wrapper {
    width: 451px;
    height: 488px;
    position: relative;
    background: 
    url("@/assets/image/EnergyPanel/矩形 21 拷贝 10.png") no-repeat
        center/cover,
      // url("@/assets/image/EnergyPanel/矩形 507 拷贝 4.png") no-repeat
      //   center/cover,
        url("@/assets/image/deviceOverview/1111111111111.png") no-repeat
        center/cover;

    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    overflow: hidden;
  }

  .panel-content {
    width: 100%;
    height: 100%;
    padding: 10px 16px 10px 16px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  /* 时间维度切换按钮 */
  .time-dimension-tabs {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 2px;
    width: 100%;
    margin-bottom: 5px;

    .tab-item {
      width: 41.5px;
      height: 28px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;
      cursor: pointer;
      font-size: 14px;
      background: url("@/assets/image/EmergencyPanel/矩形 21 拷贝 3.png")
        no-repeat center/cover;
      background-size: cover !important;
      border-radius: 4px;
      text-align: center;
      box-sizing: border-box;
      user-select: none;
      transition: font-weight 0.2s;

      &.active {
        background: url("@/assets/image/EmergencyPanel/矩形 21 拷贝 2.png")
          no-repeat center/cover;
        background-size: cover !important;
        font-weight: bold;
      }
    }
  }

  /* 用电量统计区域 */
  .energy-stats-container {
    border-radius: 4px;
    padding: 0;
    margin-top: 0;
    position: relative;

    .stats-header {
      display: flex;
      align-items: center;
      position: relative;
      z-index: 1;
      margin-bottom: 6px;

      .header-icon {
        width: 24px;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 8px;

        img {
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
      }

      .header-title {
        font-size: 16px;
        font-weight: 500;
        color: #fff;
      }

      .total-value {
        margin-left: auto;
        color: #fff;
        font-weight: bold;
        font-size: 16px;
      }
    }

    .energy-stats {
      position: relative;
      z-index: 1;
      display: flex;
      flex-direction: column;
      gap: 8px;

      .stats-item {
        display: flex;
        align-items: center;

        .item-label {
          width: 70px;
          color: #fff;
          font-size: 14px;
          text-align: right;
          padding-right: 5px;
        }

        .progress-container {
          flex: 1;
          height: 8px;
          background: rgba(5, 30, 40, 0.6); /* 半透明深色背景 */
          border-radius: 0;
          overflow: visible;
          margin: 0 10px 0 0;
          position: relative;

          .progress-bar {
            height: 100%;
            background: linear-gradient(90deg, #ff9500 0%, #ffc800 70%);
            border-radius: 0;
            position: relative;

            &::after {
              content: "";
              position: absolute;
              right: -3px;
              top: -1px;
              height: 10px;
              width: 6px;
              background: radial-gradient(
                ellipse at left,
                rgba(255, 199, 0, 0.8) 0%,
                rgba(255, 199, 0, 0) 70%
              );
            }
          }
        }

        .item-value {
          width: 70px;
          color: #fff;
          font-size: 14px;
          text-align: right;
        }
      }
    }
  }

  /* 用电量趋势区域 */
  .energy-trend-container {
    border-radius: 4px;
    padding: 0;
    margin-top: 23px;
    position: relative;

    .trend-header {
      display: flex;
      align-items: center;
      position: relative;
      z-index: 1;
      margin-bottom: 19px;

      .header-icon {
        width: 24px;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 8px;

        img {
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
      }

      .header-title {
        font-size: 16px;
        font-weight: 500;
        color: #fff;
      }
    }

    .trend-chart {
      position: relative;
      z-index: 1;
      display: flex;
      flex-direction: row;
      height: 140px;
      margin-bottom: 0;

      .trend-chart-container {
        width: 100%;
        height: 100%;
      }
    }

    .chart-x-axis {
      display: none; // 不再需要，由echarts处理
    }
  }
}
</style>
